<?php
use \PsiFramework\Classes\Util\UI\Printer;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Util\Log\Logger;

$theme = $response->getAttribute("object");
$objectType = $response->getAttribute("objectType");
?>
<form
    class="editForm"
    method="POST"
    action="<?php Printer::display(HTML::linkHref('action=CMS.Save&type='.$objectType));?>"
    enctype="multipart/form-data">
    <?php
    if ($theme != null) {
    ?>
    <input
        type="hidden"
        name="id"
        value="<?php Printer::display($theme->getId()); ?>" />
    <?php
    }
    ?>
    <div class="label">
        <span class="titre">Nom :</span>
        <input
            class="firstInput"
            type="text"
            name="name"
            maxlength="20"
            value="<?php if ($theme != null) Printer::display($theme->getName()); ?>" />
    </div>
    <div class="label">
        <span class="titre">Titre du thème :</span>
        <input
            class="firstInput"
            type="text"
            name="title"
            value="<?php if ($theme != null) Printer::display($theme->getTitle()); ?>" />
    </div>
    <div class="label">
        <span class="titre">Champs :</span>
        <div class="formPartsOrderer" id="fields">
            <ul class="formPartsList">
            <?php 
            if (!is_null($theme)) {
                $logger = new Logger("profile");
                $parts = $theme->getFields();
                $parts = $theme->sortFields();

                if (!is_null($theme) && !is_null($parts)) {
                    for ($i=0; $i<count($parts); $i++) {
                        $part = $parts[$i] ;
                        $field = $part->getField();
                        $group = $part->getGroupName();
                        $groupTitle = $part->getGroupTitle();
            ?>
                <li>
                    <span class="item">
                        <?php Printer::display($field->getName()); ?>
                        <input
                            class="id"
                            type="hidden"
                            name="part_id[]"
                            value="<?php Printer::display($field->getId()); ?>" />
                         - 
                         <label>
                             Groupe : 
                             <input
                                 type="text"
                                 name="group[]" size="10"
                                 value="<?php Printer::display($group); ?>"
                                 />
                        </label>
                        - 
                         <label>
                             Bouton : 
                             <input
                                 type="text"
                                 name="group_title[]" size="20"
                                 value="<?php Printer::display($groupTitle); ?>"
                                 />
                        </label>
                        <input
                            class="order"
                            type="hidden"
                            name="part_order[]"
                            value="<?php Printer::display($i+1); ?>" />
                    </span>
                </li>
            <?php 
                    }
                }
            }
            ?>
            </ul>
        </div>
    </div>
    <div class="label">
        <span class="titre">Titre de la présentation :</span>
        <input
            class="firstInput"
            type="text"
            name="presentation_title"
            value="<?php if ($theme != null) Printer::display($theme->getPresentationTitle()); ?>" />
    </div>
    <div class="label">
        <span class="titre">Présentation :</span>
        <textarea
            class="ckeditor"
            name="presentation"><?php
            if ($theme != null) Printer::display($theme->getPresentation());
            ?></textarea>
    </div>
    <div class="label">
        <span class="titre">Image :</span>
        <input
            class="firstInput"
            type="file"
            name="image" />
        <?php 
        if ($theme != null) {
            $image = $theme->getImage();
            if (!is_null($image)) {
            	$filePath = $image->serialize();
            	if (!isNullOrEmptyString($filePath)) {
            		$realPath = str_replace(getWwwDirectory().'/', '', realpath($filePath));
            		echo '<img src="'.Printer::getText($realPath).'" width="100" />' ;
            		
            		echo '<input '.
            				'type="hidden" '.
            				'name="current_image" '.
            				'value="'.Printer::getText($filePath).'" />' ;
            		echo '<label>'.
            				'<input '.
            				'type="checkbox" '.
            				'name="delete_image" />'.
            		        'Supprimer'.
            		    '</label>' ;
            	}
            }
        }
        ?>
    </div>
    <div class="label">
        <span class="titre">Titre du complément :</span>
        <input
            class="firstInput"
            type="text"
            name="complement_title"
            value="<?php if ($theme != null) Printer::display($theme->getComplementTitle()); ?>" />
    </div>
    <div class="label">
        <span class="titre">Complément :</span>
        <textarea
            class="ckeditor"
            name="complement"><?php
            if ($theme != null) Printer::display($theme->getComplement());
            ?></textarea>
    </div>
    <div class="label">
        <span class="titre">Image complémentaire :</span>
        <input
            class="firstInput"
            type="file"
            name="complement_image" />
        <?php 
        if ($theme != null) {
            $image = $theme->getComplementImage();
            if (!is_null($image)) {
            	$filePath = $image->serialize();
            	if (!isNullOrEmptyString($filePath)) {
            		$realPath = str_replace(getWwwDirectory().'/', '', realpath($filePath));
            		echo '<img src="'.Printer::getText($realPath).'" width="100" />' ;
            		
            		echo '<input '.
            				'type="hidden" '.
            				'name="current_complement_image" '.
            				'value="'.Printer::getText($filePath).'" />' ;
            		echo '<label>'.
            				'<input '.
            				'type="checkbox" '.
            				'name="delete_complement_image" />'.
            		        'Supprimer'.
            		    '</label>' ;
            	}
            }
        }
        ?>
    </div>
    <div class="label">
        <span class="titre">Images du slider :</span>
        <div class="multipleImages" id="sliderImages">
            <ul class="multipleImagesList">
            <?php 
            if (!is_null($theme)) {
                $images = $theme->getSliderImages();
                if (!is_null($images)) {
                    for ($i=0; $i<$images->size(); $i++) {
                        $currentImage = $images->getImage($i);
            ?>
                <li>
                    <span class="item">
                        <input
                            class="firstInput"
                            type="file"
                            name="slider_image_<?php Printer::display($i); ?>" />
                        <img src="<?php Printer::display($currentImage->getImageLink()); ?>" width="100" />
                		<input
            				type="hidden"
            				name="current_slider_image_<?php Printer::display($i); ?>"
            				value="<?php Printer::display($currentImage->getFileLink()); ?>" />
            		    <label>
            				<input
            				    type="checkbox"
            				    name="delete_slider_image_<?php Printer::display($i); ?>" />
            		            Supprimer
            		    </label>
                    </span>
                </li>
            <?php
                    }
                }
            }
            ?>
            </ul>
        </div>
    </div>
    <input type="submit" value="OK" />
</form>
<script type="text/javascript">
    $(document).ready(function() {
        $("#fields .formPartsList").formPartsOrderer(
            {
                "serviceUrl":"?service=ListFormFields&format=json",
                "container":"#fields",
                "fields":[
                	{
                        "type":"text",
                        "name":"group",
                        "label":"Groupe",
                        "size":"10"
                    },
                    {
                        "type":"text",
                        "name":"group_title",
                        "label":"Bouton",
                        "size":"20"
                    }
                ]
            }
        );

        $("#sliderImages .multipleImagesList").multipleImages('slider_image');

    	$('.ckeditor').ckeditor();
    });
</script>
